<template>
  <transition name="el-zoom-in-center">
    <div class="JNPF-preview-main flow-form-main">
      <div class="JNPF-common-page-header">
        <el-page-header @back="goBack" :content="!dataForm.id ? '新建等级' : '编辑等级'" />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">
            {{$t('common.confirmButton')}}</el-button>
          <el-button @click="goBack()">{{$t('common.cancelButton')}}</el-button>
        </div>
      </div>
      <el-row class="main" v-loading="loading">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px"
          @submit.native.prevent>
          <el-col :span="12">
            <el-form-item label="会员等级" prop="levelCode">
              <el-select v-model="dataForm.levelCode" placeholder="请选择等级" clearable @change="onMsgTypeChange">
                <el-option v-for="(item,index) in levelCodeList" :key="index"
                  :label="item.fullName" :value="item.enCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级名称" prop="levelName">
              <el-input v-model="dataForm.levelName" placeholder="请输入等级名称" :maxlength="10" show-word-limit clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会费金额" prop="amount">
              <el-input v-model="dataForm.amount" :maxlength="20" show-word-limit placeholder="请输入会费" clearable>
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级状态" prop="enabledMark">
              <el-select v-model="dataForm.enabledMark" placeholder="请选择等级状态" clearable>
                <el-option v-for="(item,index) in enabledMarkList" :key="index"
                  :label="item.fullName" :value="item.enCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员期限" prop="vipPeriod">
              <el-input v-model="dataForm.vipPeriod" :maxlength="20" show-word-limit placeholder="请输入会员期限" clearable>
                <template slot="append">月</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
  </transition>
</template>
<script>
import { vipLevelInfo, addVipLevel, editVipLevel, getVipGrade } from '@/api/asso/vip/vipLevel'
export default {
  name: 'vipLevel-Form',
  data() {
    return {
      dataForm: {
        id: '',
        levelCode: '',
        levelName: '',
        amount: '',
        enabledMark: '',
        vipPeriod: ''
      },
      dataRule: {
        levelCode: [
          { required: true, message: '会员等级不能为空', trigger: 'change' },
        ],
        levelName: [
          { required: true, message: '等级名称不能为空', trigger: 'blur' },
        ],
        amount: [
          { required: true, message: '会费金额不能为空', trigger: 'blur' },
        ],
        enabledMark: [
          { required: true, message: '等级状态不能为空', trigger: 'change' },
        ],
        vipPeriod: [
          { required: true, message: '会员期限不能为空', trigger: 'blur' },
        ]
      },
      btnLoading: false,
      loading: false,
      enabledMarkList: [
        {
          fullName: '正常',
          enCode: 1
        },
        {
          fullName: '禁用',
          enCode: 0
        }
      ],
      levelCodeList: [],
      isEdit: false,
      keyword: "",
      parameterList: [],
      allParameterList: [],
      smsList: []
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.parameterList = []
      this.allParameterList = []
      this.getVipGrade()
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.loading = true
          vipLevelInfo(this.dataForm.id).then(res => {
            this.loading = false
            this.dataForm = res.data
            this.parameterList = res.data.templateParamList
            this.allParameterList = this.parameterList
            this.smsList = res.data.smsFieldListgetConfig
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    // 获取会员等级列表数据
    getVipGrade(){
      getVipGrade('366232797372744005').then(res => {
        if(res.code == 200){
          this.levelCodeList = res.data.list
        }
      })
    },
    // 新增、编辑
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if(this.dataForm.amount <= 0 || isNaN(this.dataForm.amount)){
             this.$message.error('会费金额请输入正数')
             return
          }
          if(this.dataForm.vipPeriod < 0 || isNaN(this.dataForm.vipPeriod)){
            this.$message.error('会员期限请输入正数')
            return
          }
          this.btnLoading = true
          if (!!this.dataForm.id) {
            editVipLevel(this.dataForm.id, this.dataForm).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.btnLoading = false
                  this.$emit('close', true)
                }
              })
            }).catch(() => { this.btnLoading = false })
          } else {
            addVipLevel(this.dataForm).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.btnLoading = false
                  this.$emit('close', true)
                }
              })
            }).catch(() => { this.btnLoading = false })
          }
        }
      })
    },
    onMsgTypeChange(e) {
      this.dataForm.content = ''
    },
    goBack() {
      this.$emit('close')
    }
  }
}
</script>